package com.action;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import com.dao.CountyDAO;
import com.dao.GovernmentDAO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.pojo.County;
import com.pojo.Government;
import com.vo.GovernmentVO;

public class GovernmentAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6416197943267969900L;

	private List<GovernmentVO> list = null;
	private GovernmentVO goVO = null;
	private List<County> listCounty = new ArrayList<County>();
	private String inActive = "false";
	private String listInactiveDir;
	private String errname;

	public String getErrname() {
		return errname;
	}

	public void setErrname(String errname) {
		this.errname = errname;
	}

	public String getInActive() {
		return inActive;
	}

	public void setInActive(String inActive) {
		this.inActive = inActive;
	}

	public String getListInactiveDir() {
		return listInactiveDir;
	}

	public void setListInactiveDir(String listInactiveDir) {
		this.listInactiveDir = listInactiveDir;
	}

	public List<County> getListCounty() {
		return listCounty;
	}

	public void setListCounty(List<County> listCounty) {
		this.listCounty = listCounty;
	}

	public GovernmentAction() {
		list = new ArrayList<GovernmentVO>();
		goVO = new GovernmentVO();
		listCounty = new ArrayList<County>();
	}

	public GovernmentAction(List<GovernmentVO> list, GovernmentVO goVO,
			List<County> listCounty) {
		super();
		this.list = list;
		this.goVO = goVO;
	}

	public List<GovernmentVO> getList() {
		return list;
	}

	public void setList(List<GovernmentVO> list) {
		this.list = list;
	}

	public GovernmentVO getGoVO() {
		return goVO;
	}

	public void setGoVO(GovernmentVO goVO) {
		this.goVO = goVO;
	}

	public String listAction() throws Exception {
		String isActive;
		if (inActive.equals("false")) {
			isActive = "Yes";
		} else
			isActive = "No";
		list = new GovernmentDAO().list(isActive);
		return SUCCESS;
	}

	public String filterIsActive() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		String filterName = request.getParameter("filterName");
		request.getSession().setAttribute("filter", filterName);
		String isActive;
		if (inActive.equals("false")) {
			isActive = "Yes";
		} else
			isActive = "No";
		list = new GovernmentDAO().getByFilterName(filterName, isActive);
		return SUCCESS;
	}

	public String insertAction() throws Exception {
		if (!(new GovernmentDAO().checkGovernmentName(goVO.getGovernmentName()))) {
			setErrname("Name is exis");
			return ERROR;
		} else {
			County c = new CountyDAO().getCountyByCountyName(goVO
					.getCountyName());
			Government g = new Government();
			if (goVO.getIsActive().equals("true")) {
				goVO.setIsActive("Yes");
			} else
				goVO.setIsActive("No");
			g.setGovernmentId(goVO.getGovernmentId());
			g.setGovernmentName(goVO.getGovernmentName());
			g.setDescription(goVO.getDescription());
			g.setCounty(c);
			g.setIsActive(goVO.getIsActive());
			new GovernmentDAO().insert(g);
			return SUCCESS;
		}
	}

	public String addAction() {
		return SUCCESS;
	}

	public String editAction() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		int governmentId = Integer.parseInt(request
				.getParameter("governmentId"));
		goVO = new GovernmentDAO().getGovernmentById(governmentId);
		if (goVO.getIsActive().equals("Yes")) {
			goVO.setIsActive("true");
		} else
			goVO.setIsActive("false");
		return SUCCESS;
	}

	public String updateAction() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		int governmentId = Integer.parseInt(request
				.getParameter("governmentId"));
		if (goVO.getIsActive().equals("true")) {
			goVO.setIsActive("Yes");
		} else
			goVO.setIsActive("No");
		new GovernmentDAO().updateGoverment(goVO, governmentId);
		return SUCCESS;
	}

	public String deleteAction() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
				.getContext().get(ServletActionContext.HTTP_REQUEST);
		int governmentId = Integer.parseInt(request
				.getParameter("governmentId"));
		new GovernmentDAO().delete(governmentId);
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String listCounty() {
		listCounty = (List<County>) new CountyDAO().listCounty();
		return SUCCESS;
	}
}
